package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.constants.message.UpdateArticleMess;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dto.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 收藏 取消收藏
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectArticle(CollectionBehaviorDTO dto) {
        //TODO 参数校验
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"请先登录");
        }
        //todo 查询实体 根据userid 或设备id
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdAndEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"行为实体数据不存在");
        }

        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId())
                .and("articleId").is(dto.getArticleId()));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);

        //判断操作类型是否是收藏，查询是否已经收藏
        if (dto.getOperation().intValue()==0&&apCollection!=null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"你已经收藏");
        }
        if (dto.getOperation().intValue()==0){
             apCollection = new ApCollection();
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setType((short)0);
            apCollection.setCollectionTime(new Date());
            mongoTemplate.save(apCollection);
        }else {
            mongoTemplate.remove(query,ApCollection.class);
        }
        //TODO **********收藏行为MQ**************
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setAdd(dto.getOperation()==0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE,  JSON.toJSONString(updateArticleMess));
        log.info("收藏行为 发送消息 ==>{}",JSON.toJSONString(updateArticleMess));
        return ResponseResult.okResult();
    }
}
